package city.spring.modules.medicine.service.impl;

import city.spring.modules.medicalstation.drugwarehouse.service.DrugwarehouseService;
import city.spring.modules.medicine.entity.MedicinedeliveryEntity;
import city.spring.modules.medicine.entity.MedicinedeliveryMedicineExtEntity;
import city.spring.modules.medicine.entity.MedicinereceiptEntity;
import city.spring.modules.medicine.repository.MedicinedeliveryMedicineExtRepository;
import city.spring.modules.medicine.repository.MedicinedeliveryRepository;
import city.spring.modules.medicine.service.MedicinedeliveryService;
import city.spring.modules.medicine.service.MedicinereceiptService;
import city.spring.modules.medicine.vo.MedicinedeliveryVO;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NonNull;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service：药品出库表
 *
 * @author 谢金婷
 * @date 2020-07-08 16:16:50
 */
@CacheConfig(cacheNames = {MedicinedeliveryServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class MedicinedeliveryServiceImpl extends ServiceImpl<MedicinedeliveryRepository, MedicinedeliveryEntity> implements MedicinedeliveryService {

    private final MedicinedeliveryMedicineExtRepository deliveryMedicineExtRepository;
    private final MedicinereceiptService medicinereceiptService;
    private final UserService userService;
    private final MedicinedeliveryRepository medicinedeliveryRepository;
    private final DrugwarehouseService medicinewarehouseService;

    public MedicinedeliveryServiceImpl(MedicinedeliveryMedicineExtRepository deliveryMedicineExtRepository, MedicinereceiptService medicinereceiptService, UserService userService, MedicinedeliveryRepository medicinedeliveryRepository, DrugwarehouseService medicinewarehouseService) {
        this.deliveryMedicineExtRepository = deliveryMedicineExtRepository;
        this.medicinereceiptService = medicinereceiptService;
        this.userService = userService;
        this.medicinedeliveryRepository = medicinedeliveryRepository;
        this.medicinewarehouseService = medicinewarehouseService;
    }

    @Override
    public void saveMedicinedelivery(MedicinedeliveryEntity entity, @NonNull Principal user) {


        /**
         * 出库信息
         */
        // 出库时间=当前系统时间
        entity.setDeliverydate(new Date());
        // 获取领用单信息
        MedicinereceiptEntity medicinereceiptEntity = medicinereceiptService.getById(entity.getMedicinereceiptId());
        System.out.println(medicinereceiptEntity);
        // 领用药品id
        entity.setMedicineId(Integer.valueOf(medicinereceiptEntity.getMedicineId()));
        //领用人
        entity.setUserId(medicinereceiptEntity.getReceipter());
        // 领用单
        entity.setMedicinereceiptId(medicinereceiptEntity.getId());
        //登记人
        //当前用户
        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
        entity.setUsersId(userEntity.getId());

        /**
         * 更新领用单信息
         */
        medicinereceiptEntity.setDeliveryStatus(1);
        // 更新领用单信息
        medicinereceiptService.lambdaUpdate()
                .set(MedicinereceiptEntity::getDeliveryStatus, medicinereceiptEntity.getDeliveryStatus())
                .eq(MedicinereceiptEntity::getId, medicinereceiptEntity.getId())
                .update();

        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
//        System.out.println("entity.getId()" + entity.getId());
    }

    @Override
    public void updateMedicinedelivery(MedicinedeliveryEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMedicinedelivery(String primaryKey) {
        removeById(primaryKey);
        // 删除包括的出库物资表的信息
        List<MedicinedeliveryMedicineExtEntity> medicinedeliveryMaterials = deliveryMedicineExtRepository.getByDeliveryId(primaryKey);
        System.out.println("medicinedeliveryMaterials" + medicinedeliveryMaterials);
        if (!medicinedeliveryMaterials.isEmpty()) {
            List<String> medicinedeliveryId = medicinedeliveryMaterials.stream().map(MedicinedeliveryMedicineExtEntity::getId).collect(Collectors.toList());
            deliveryMedicineExtRepository.deleteBatchIds(medicinedeliveryId);
        }
    }

    @Override
    public void deleteMedicinedelivery(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public MedicinedeliveryVO getMedicinedeliveryById(String id) {
        MedicinedeliveryEntity deliveryEntity = medicinedeliveryRepository.getById(id);
        System.out.println("deliveryEntity" + deliveryEntity);
        MedicinedeliveryVO deliveryVO = new MedicinedeliveryVO(deliveryEntity);

        // 加载物资列表
        deliveryVO.setMedicines(deliveryMedicineExtRepository.getDeliveryMedicines(Integer.valueOf(id)));

        return deliveryVO;
    }

    @Override
    public IPage<MedicinedeliveryVO> listPage(Page<? extends MedicinedeliveryEntity> page, Wrapper<? extends MedicinedeliveryEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }
}